#!/usr/bin/python

# includes pygame and the like
from includes import * 

from State import *
from menuState import *
from gameState import *
from Stack import *

screenW = 1024
screenH = 768

def run():
	# init pygame and get a display 
	pygame.init();
	screen = pygame.display.set_mode((screenW,screenH),SWSURFACE)
	
	# new instance of the Stack object and push the menu state onto the stack
	machine = Stack()
	machine.push(menuState())	
	
	# if the stack has a state on it then run
	while machine.ready() == 1:
		
		# get the current running state
		currentState = machine.top();
		
		# see if the state needs to be inited if so do it.
		if machine.needInit():
			currentState.init()
			
		# call the update and draw functions in the currentState
		currentState.update();
		currentState.draw();
		
		# if the currentState has a state waiting to be pushed on top, handle it
		if currentState.nextState:
			machine.push(currentState.nextState)
			currentState.nextState = 0
			currentState = machine.top();
			#machine.dump()
			
		# check to see if a state is still alive.
		if currentState.run == 0:
			machine.pop()
			#machine.dump()
			
			# see if there is still a state if not exit.
			if machine.top():
				currentState = machine.top();
			else:
				pygame.quit()
				return 0

run()
